package com.dpteam.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dpteam.bean.CountPie;
import com.dpteam.dao.*;
import com.dpteam.model.Category;
import com.dpteam.model.ImportProduct;
import com.dpteam.service.StatisticsService;
import com.dpteam.util.OptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/13
 * Time: 19:09
 */
@Service
@Transactional
public class StatisticsServiceImpl implements StatisticsService {

    private static final String PROFIT = "净利润";

    private static final String TURNOVER = "营业额";

    private static final String IMPORT_NUMBER = "入库总数量";

    private static final String IMPORT_COST = "入库耗资";

    private static final String IMPORT_NUMBER_CATEGORY = "指定分类入库数量";

    private static final String IMPORT_COST_CATEGORY = "指定分类入库耗资";

    @Resource
    private OrderDao orderDao;

    @Resource
    private OrderItemDao orderItemDao;

    @Resource
    private RejectOrderDao rejectOrderDao;

    @Resource
    private ImportProductDao importProductDao;

    @Resource
    private CategoryDao categoryDao;

    private static Logger logger = LoggerFactory.getLogger(StatisticsServiceImpl.class);

    @Override
    public String statisticsImportNumber(int year) {
        logger.info("统计 [{}] 年 1-12月商品入库总数量 (折线图,柱状图)", year);
        String title = year + "年商品入库总数量";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        int[] counts = new int[12];
        for (int month = 1; month <= 12; month++) {
            counts[month -1] = importProductDao.countAmount(year, month);
        }
        JSONObject option = new JSONObject();

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray array = new JSONArray();
        for (String s : xAxisData) {
            array.add(s);
        }
        xTemp.put(OptionUtil.data, array);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //series数据
        JSONObject yTemp = new JSONObject();
        yTemp.put(OptionUtil.name, IMPORT_NUMBER);
        yTemp.put(OptionUtil.type, OptionUtil.bar);
        JSONArray ydata = new JSONArray();
        for (int count: counts) {
            ydata.add(count);
        }
        yTemp.put(OptionUtil.data, ydata);
        JSONArray series = new JSONArray();
        series.add(yTemp);
        option.put(OptionUtil.series, series);

        //legend
        JSONArray legendData = new JSONArray();
        legendData.add(IMPORT_NUMBER);
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.data, legendData);
        option.put(OptionUtil.legend, legend);


        //标题
        option.put(OptionUtil.title, title);

        return option.toJSONString();
    }

    @Override
    public String statisticsImportCost(int year) {
        logger.info("统计 [{}] 年 商品入库总耗资 (折线图,柱状图)", year);
        String title = year + "年 1-12月商品入库总耗资";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        double[] costs = new double[12];
        for (int month = 1; month <= 12; month++) {
            costs[month -1] = importProductDao.countCost(year, month);
        }

        JSONObject option = new JSONObject();

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray array = new JSONArray();
        for (String s : xAxisData) {
            array.add(s);
        }
        xTemp.put(OptionUtil.data, array);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //series数据
        JSONObject yTemp = new JSONObject();
        yTemp.put(OptionUtil.name, IMPORT_COST);
        yTemp.put(OptionUtil.type, OptionUtil.bar);
        JSONArray ydata = new JSONArray();
        for (double cost: costs) {
            ydata.add(cost);
        }
        yTemp.put(OptionUtil.data, ydata);
        JSONArray series = new JSONArray();
        series.add(yTemp);
        option.put(OptionUtil.series, series);

        //legend
        JSONArray legendData = new JSONArray();
        legendData.add(IMPORT_COST);
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.data, legendData);
        option.put(OptionUtil.legend, legend);

        //标题
        option.put(OptionUtil.title, title);

        return option.toJSONString();
    }

    @Override
    public String statisticsImportNumber(int year, int categoryId) {
        logger.info("统计 [{}] 年 1-12月 指定商品分类 :[{}] 商品入库总数量 (折线图,柱状图)", year, categoryId);
        Category category = categoryDao.get(Category.class, categoryId);
        String title = year + "年" +category.getTypeName() + "商品分类入库总数量";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        int[] counts = new int[12];
        for (int month = 1; month <= 12; month++) {
            counts[month - 1] = importProductDao.countAmount(year, month, categoryId);
        }
        JSONObject option = new JSONObject();

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray array = new JSONArray();
        for (String s : xAxisData) {
            array.add(s);
        }
        xTemp.put(OptionUtil.data, array);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //series数据
        JSONObject yTemp = new JSONObject();
        yTemp.put(OptionUtil.name, IMPORT_NUMBER);
        yTemp.put(OptionUtil.type, OptionUtil.bar);
        JSONArray ydata = new JSONArray();
        for (int count: counts) {
            ydata.add(count);
        }
        yTemp.put(OptionUtil.data, ydata);
        JSONArray series = new JSONArray();
        series.add(yTemp);
        option.put(OptionUtil.series, series);

        //legend
        JSONArray legendData = new JSONArray();
        legendData.add(IMPORT_NUMBER_CATEGORY);
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.data, legendData);
        option.put(OptionUtil.legend, legend);

        //标题
        option.put(OptionUtil.title, title);

        return option.toJSONString();
    }

    @Override
    public String statisticsImportCost(int year, int categoryId) {
        logger.info("统计 [{}] 年指定商品分类 :[{}] 商品入库总耗资 (折线图,柱状图)", year, categoryId);
        String title = year + "年 1-12月商品分类入库总耗资";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        double[] costs = new double[12];
        for (int month = 1; month <= 12; month++) {
            costs[month - 1] = importProductDao.countCost(year, month, categoryId);
        }
        JSONObject option = new JSONObject();

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray array = new JSONArray();
        for (String s : xAxisData) {
            array.add(s);
        }
        xTemp.put(OptionUtil.data, array);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //series数据
        JSONObject yTemp = new JSONObject();
        yTemp.put(OptionUtil.name, IMPORT_COST_CATEGORY);
        yTemp.put(OptionUtil.type, OptionUtil.bar);
        JSONArray ydata = new JSONArray();
        for (double cost: costs) {
            ydata.add(cost);
        }
        yTemp.put(OptionUtil.data, ydata);
        JSONArray series = new JSONArray();
        series.add(yTemp);
        option.put(OptionUtil.series, series);

        //legend
        JSONArray legendData = new JSONArray();
        legendData.add(IMPORT_NUMBER_CATEGORY);
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.data, legendData);
        option.put(OptionUtil.legend, legend);

        //标题
        option.put(OptionUtil.title, title);

        return option.toJSONString();
    }

    @Override
    public String statisticsProfit(int year) {
        logger.info("统计 [{}] 年份净利润 (折线图,柱状图)", year);
        String title = year + "年 1-12月商品净利润";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        double[] profits = new double[12];
        for (int month = 1; month <= 12; month++) {
            profits[month - 1] = orderDao.sumProfit(year, month) - rejectOrderDao.sumProfit(year, month);
        }

        JSONObject option = new JSONObject();

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray array = new JSONArray();
        for (String s : xAxisData) {
            array.add(s);
        }
        xTemp.put(OptionUtil.data, array);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //series数据
        JSONObject yTemp = new JSONObject();
        yTemp.put(OptionUtil.name, IMPORT_COST_CATEGORY);
        yTemp.put(OptionUtil.type, OptionUtil.line);
        JSONArray ydata = new JSONArray();
        for (double profit: profits) {
            ydata.add(profit);
        }
        yTemp.put(OptionUtil.data, ydata);
        JSONArray series = new JSONArray();
        series.add(yTemp);
        option.put(OptionUtil.series, series);

        //legend
        JSONArray legendData = new JSONArray();
        legendData.add(PROFIT);
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.data, legendData);
        option.put(OptionUtil.legend, legend);

        //标题
        option.put(OptionUtil.title, title);

        return option.toJSONString();
    }

    @Override
    public String statisticsTurnover(int year) {
        logger.info("统计 [{}] 年份1-12月营业额 (折线图,柱状图)", year);
        String title = year + "年营业额";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        double[] turnover = new double[12];
        for (int month = 1; month <= 12; month++) {
            turnover[month - 1] = orderDao.sumSell(year, month) - rejectOrderDao.sumSell(year, month);
        }

        JSONObject option = new JSONObject();

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray array = new JSONArray();
        for (String s : xAxisData) {
            array.add(s);
        }
        xTemp.put(OptionUtil.data, array);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //series数据
        JSONObject yTemp = new JSONObject();
        yTemp.put(OptionUtil.name, IMPORT_COST_CATEGORY);
        yTemp.put(OptionUtil.type, OptionUtil.line);
        JSONArray ydata = new JSONArray();
        for (double turn: turnover) {
            ydata.add(turn);
        }
        yTemp.put(OptionUtil.data, ydata);
        JSONArray series = new JSONArray();
        series.add(yTemp);
        option.put(OptionUtil.series, series);

        //legend
        JSONArray legendData = new JSONArray();
        legendData.add(TURNOVER);
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.data, legendData);
        option.put(OptionUtil.legend, legend);

        //标题
        option.put(OptionUtil.title, title);

        return option.toJSONString();
    }

    @Override
    public String statisticProductProfit(int year) {
        logger.info("统计 [{}] 年 [{}] 月 不同商品分类的净利润 (柱状图)", year);
        String title = "不同分类商品利润";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        List<Category> categories = categoryDao.findAll();

        JSONObject option = new JSONObject();

        //series
        JSONArray array = new JSONArray();
        JSONArray legendData = new JSONArray();
        for (Category category : categories) {
            JSONObject temp = new JSONObject();
            int categoryId = category.getId();
            JSONArray jsonArray1 = new JSONArray();
            for (int month = 1; month <= 12; month++) {
                double profit = orderItemDao.sumProfit(year, month, categoryId) - rejectOrderDao.sumProfit(year, month, categoryId);
                jsonArray1.add(profit);
            }
            temp.put("data", jsonArray1);
            temp.put("type", "line");
            temp.put("name", category.getTypeName());
            array.add(temp);
            legendData.add(category.getTypeName());
        }
        option.put("series", array);

        //legend
        JSONObject legend = new JSONObject();
        legend.put("data", legendData);
        option.put("legend", legend);

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray xData = new JSONArray();
        for (String s : xAxisData) {
            xData.add(s);
        }
        xTemp.put(OptionUtil.data, xData);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //标题
        option.put(OptionUtil.title, title);
        return option.toJSONString();
    }

    @Override
    public String statisticProductTurnover(int year) {
        logger.info("统计 [{}] 年 [{}] 月 不同商品分类的营业额(柱状图)", year);
        String title = "不同分类商品营业额";
        String[] xAxisData = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        List<Category> categories = categoryDao.findAll();

        JSONObject option = new JSONObject();

        //series
        JSONArray array = new JSONArray();
        JSONArray legendData = new JSONArray();
        for (Category category : categories) {
            JSONObject temp = new JSONObject();
            int categoryId = category.getId();
            JSONArray jsonArray1 = new JSONArray();
            for (int month = 1; month <= 12; month++) {
                double profit = orderItemDao.sumSell(year, month, categoryId) - rejectOrderDao.sumSell(year, month, categoryId);
                jsonArray1.add(profit);
            }
            temp.put("data", jsonArray1);
            temp.put("type", "line");
            temp.put("name", category.getTypeName());
            array.add(temp);
            legendData.add(category.getTypeName());
        }
        option.put("series", array);

        //legend
        JSONObject legend = new JSONObject();
        legend.put("data", legendData);
        option.put("legend", legend);

        //xAxis数据
        JSONObject xTemp = new JSONObject();
        JSONArray xAxis = new JSONArray();
        xTemp.put(OptionUtil.type, OptionUtil.category);
        JSONArray xData = new JSONArray();
        for (String s : xAxisData) {
            xData.add(s);
        }
        xTemp.put(OptionUtil.data, xData);
        xAxis.add(xTemp);
        option.put(OptionUtil.xAxis, xAxis);

        //标题
        option.put(OptionUtil.title, title);
        return option.toJSONString();
    }

    @Override
    public String statisticsProfitPie(int year) {
        logger.info("统计 [{}] 年，不同商品类别的净利润饼图", year);
        List<CountPie> pies = orderItemDao.countProfit(year);

        JSONObject option = new JSONObject();

        //title
        JSONObject title = new JSONObject();
        title.put(OptionUtil.text, year + "年 净利润");
        title.put(OptionUtil.subtext, "各分类占百分比");
        title.put(OptionUtil.x, "center");
        option.put(OptionUtil.title, title);

        //legend
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.orient, OptionUtil.vertical);
        legend.put(OptionUtil.x, OptionUtil.left);
        JSONArray legendData = new JSONArray();

        //series
        JSONArray series = new JSONArray();
        JSONObject temp = new JSONObject();
        temp.put(OptionUtil.name, "净利润");
        temp.put(OptionUtil.type, OptionUtil.pie);
        temp.put("radius", "55");
        JSONArray seriesData = new JSONArray();

        for (CountPie pie : pies) {
            legendData.add(pie.getTypeName());
            JSONObject json = new JSONObject();
            json.put(OptionUtil.name, pie.getTypeName());
            json.put("value", pie.getProfit());
            seriesData.add(json);
        }

        legend.put(OptionUtil.data, legendData);
        temp.put(OptionUtil.data, seriesData);
        series.add(temp);

        option.put(OptionUtil.legend, legend);
        option.put(OptionUtil.series, series);
        return option.toJSONString();
    }

    @Override
    public String statisticsProfitPie(int year, int month) {
        logger.info("统计 [{}] 年 [{}] 月 不同商品类别的净利润饼图", year, month);
        List<CountPie> pies = orderItemDao.countProfit(year, month);

        JSONObject option = new JSONObject();

        //title
        JSONObject title = new JSONObject();
        title.put(OptionUtil.text, year + "年" + month + "月" + "的净利润");
        title.put(OptionUtil.subtext, "不同分类百分比");
        title.put(OptionUtil.x, "center");
        option.put(OptionUtil.title, title);

        //legend
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.orient, OptionUtil.vertical);
        legend.put(OptionUtil.x, OptionUtil.left);
        JSONArray legendData = new JSONArray();

        //series
        JSONArray series = new JSONArray();
        JSONObject temp = new JSONObject();
        temp.put(OptionUtil.name, "净利润");
        temp.put(OptionUtil.type, OptionUtil.pie);
        temp.put("radius", "55");
        JSONArray seriesData = new JSONArray();

        for (CountPie pie : pies) {
            legendData.add(pie.getTypeName());
            JSONObject json = new JSONObject();
            json.put(OptionUtil.name, pie.getTypeName());
            json.put("value", pie.getProfit());
            seriesData.add(json);
        }

        legend.put(OptionUtil.data, legendData);
        temp.put(OptionUtil.data, seriesData);
        series.add(temp);

        option.put(OptionUtil.legend, legend);
        option.put(OptionUtil.series, series);
        return option.toJSONString();
    }

    @Override
    public String statisticsTurnoverPie(int year) {
        logger.info("统计 [{}] 年，不同商品类别的营业额饼图", year);
        List<CountPie> pies = orderItemDao.countSell(year);

        JSONObject option = new JSONObject();

        //title
        JSONObject title = new JSONObject();
        title.put(OptionUtil.text, year + "年 营业额");
        title.put(OptionUtil.subtext, "各分类占百分比");
        title.put(OptionUtil.x, "center");
        option.put(OptionUtil.title, title);

        //legend
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.orient, OptionUtil.vertical);
        legend.put(OptionUtil.x, OptionUtil.left);
        JSONArray legendData = new JSONArray();

        //series
        JSONArray series = new JSONArray();
        JSONObject temp = new JSONObject();
        temp.put(OptionUtil.name, "营业额");
        temp.put(OptionUtil.type, OptionUtil.pie);
        temp.put("radius", "55");
        JSONArray seriesData = new JSONArray();

        for (CountPie pie : pies) {
            legendData.add(pie.getTypeName());
            JSONObject json = new JSONObject();
            json.put(OptionUtil.name, pie.getTypeName());
            json.put("value", pie.getSell());
            seriesData.add(json);
        }

        legend.put(OptionUtil.data, legendData);
        temp.put(OptionUtil.data, seriesData);
        series.add(temp);

        option.put(OptionUtil.legend, legend);
        option.put(OptionUtil.series, series);
        return option.toJSONString();
    }

    @Override
    public String statisticsTurnoverPie(int year, int month) {
        logger.info("统计 [{}] 年 [{}] 月 不同商品类别的营业额饼图", year, month);
        List<CountPie> pies = orderItemDao.countSell(year, month);

        JSONObject option = new JSONObject();

        //title
        JSONObject title = new JSONObject();
        title.put(OptionUtil.text, year + "年" + month + "月" + "的营业额");
        title.put(OptionUtil.subtext, "不同分类百分比");
        title.put(OptionUtil.x, "center");
        option.put(OptionUtil.title, title);

        //legend
        JSONObject legend = new JSONObject();
        legend.put(OptionUtil.orient, OptionUtil.vertical);
        legend.put(OptionUtil.x, OptionUtil.left);
        JSONArray legendData = new JSONArray();

        //series
        JSONArray series = new JSONArray();
        JSONObject temp = new JSONObject();
        temp.put(OptionUtil.name, "营业额");
        temp.put(OptionUtil.type, OptionUtil.pie);
        temp.put("radius", "55");
        JSONArray seriesData = new JSONArray();

        for (CountPie pie : pies) {
            legendData.add(pie.getTypeName());
            JSONObject json = new JSONObject();
            json.put(OptionUtil.name, pie.getTypeName());
            json.put("value", pie.getSell());
            seriesData.add(json);
        }

        legend.put(OptionUtil.data, legendData);
        temp.put(OptionUtil.data, seriesData);
        series.add(temp);

        option.put(OptionUtil.legend, legend);
        option.put(OptionUtil.series, series);
        return option.toJSONString();
    }
}
